<template>
  <view class="about">
    <uni-section title="用户登录" type="line">
      <view class="example">
        <uni-forms ref="customForm" :rules="customRules" :label-position="'left'" :modelValue="baseFormData">
          <uni-forms-item label="用户" required name="username">
            <uni-easyinput v-model="baseFormData.username" placeholder="请输入用户"/>
          </uni-forms-item>
          <uni-forms-item label="密码" required name="password">
            <uni-easyinput type="password" v-model="baseFormData.password" placeholder="请输入密码"/>
          </uni-forms-item>

          <button type="primary" @click="submit('customForm')">登录</button>
        </uni-forms>
      </view>
    </uni-section>
  </view>
</template>

<script>
export default {
  data() {
    return {
      baseFormData: {
        username: '',
        password: ''
      },
      customRules: {
        username: {
          rules: [{
            required: true,
            errorMessage: '用户名不能为空'
          }]
        },
        password: {
          rules: [{
            required: true,
            errorMessage: '密码不能为空'
          }]
        },
      }
    }
  },
  onLoad() {
  },
  methods: {
    submit: function (ref) {
      console.log(this.baseFormData);
      this.$refs[ref].validate().then(res => {
        const PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvrvh/BoZ6bcnCltEWSyT9WnDOlfwXOHWotOS3j1rgE50UjQ82CFKArvVj5qpTT2AJAlDYz0EzTDNXKluyPcOzxL7qJSdASjJuR5ewrXJcL/6Q51465vQqijlkYrZx/kg57Oy5eVSJKZhTpNiwAM0iJ7Mjt7gzPU4nqQCkzb8ZLvl8O/+93hrMQu4zk+QWjeBz6rqWNPZA4prOedUUUQIlsUpClL8+HuF6lhc4GiyXKIuqFv7yfKECZfdfI8+e5Awxc57sRqyTOMtQD34P5t6anYQhyTuM5CVHW/Z8q7o0MDeJ941MxmxUxlCASFbZPAfqOMBNI5UjjKs5uiB0CIvXQIDAQAB";
        const encryptor = new JSEncrypt();
        encryptor.setPublicKey(PUBLIC_KEY);
        let pwd = encryptor.encrypt(this.baseFormData.password);
        uni.request({
          method: 'POST',
          url: '/sso/doLogin',
          data: {
            name: this.baseFormData.username,
            pwd: pwd
          },
          header: {
            // 请求头（如 Content-Type、Token 等）
            'Content-Type': 'application/x-www-form-urlencoded' // 默认值，可省略
          },
          success: (res) => {
            uni.showToast({
              title: res.data.msg,
              icon: 'none',
              success: function () {
                sessionStorage.setItem("tokenValue", res.data.data.tokenValue);

                sessionStorage.setItem("userName", res.data.data.userName);
                sessionStorage.setItem("phone", res.data.data.phone);

                uni.reLaunch({
                  url: '/pages/tabBar/index/index'  // 首页的页面路径（根据实际项目路径修改）
                });
              }
            });
          },
          fail: (err) => {
            console.log('request fail', err);
            uni.showModal({
              content: err.errMsg,
              showCancel: false
            });
          },
          complete: () => {
            this.loading = false;
          }
        });


      }).catch(err => {
        console.log('err', err);
      })
    }
  }
}
</script>

<style lang="scss">
.example {
  padding: 15px;
  background-color: #fff;
}

.segmented-control {
  margin-bottom: 15px;
}

.button-group {
  margin-top: 15px;
  display: flex;
  justify-content: space-around;
}

.form-item {
  display: flex;
  align-items: center;
  flex: 1;
}

.button {
  display: flex;
  align-items: center;
  height: 35px;
  line-height: 35px;
  margin-left: 10px;
}
</style>

